package com.everflourish.yeah100.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.everflourish.yeah100.R;
import com.everflourish.yeah100.utils.ResourcesUtil;

import java.util.List;

/**
 * 单选对话框对话框
 *
 * @author p大田
 */
public class ItemsDialog extends AlertDialog implements DialogInterface {

    public static final int BUTTON_CONFIRM = -1;

    public static final int BUTTON_CANCEL = -2;

    private Context mContext;
    private TextView mTitleTv;
    private TextView mCleanTv;
    private ListView mSingleLv;
    private LinearLayout mScoreLl;
    private EditText mScoreEt;
    private Button mSuerBt;
    private String mTitle;
    private List<String> mItems;
    private static boolean ScoreIsPut = false;
    private static boolean topicType = false;

    private String mConfirmName;
    private OnClickListener onConfirmListener;

    private OnClickListener onClickListener;

    public ItemsDialog(Context context, String title, boolean topicType, boolean ScoreIsPut) {
        super(context, R.style.MyDialog);
        this.mContext = context;
        setContentView(R.layout.dialog_single);
        mTitleTv = (TextView) this.findViewById(R.id.title_tv);
        mSingleLv = (ListView) this.findViewById(R.id.single_lv);
        mScoreLl = (LinearLayout) this.findViewById(R.id.scorce_put);
        mScoreEt = (EditText) this.findViewById(R.id.scorceEt);
        mSuerBt = (Button) this.findViewById(R.id.sure);
        mCleanTv = (TextView) this.findViewById(R.id.dialog_single_clean_tv);
        mCleanTv.setVisibility(View.GONE);
        this.mTitle = title;
        this.ScoreIsPut = ScoreIsPut;
        this.topicType = topicType;
    }

    public ItemsDialog(Context context) {
        this(context, null, topicType, ScoreIsPut);
    }

    public ItemsDialog notifyDataSetChanged() {
        if (singleAdapter != null) {
            singleAdapter.notifyDataSetChanged();
        }
        return this;
    }

    /**
     * 设置确定按钮
     *
     * @param btnName         按钮名称
     * @param onClickListener 按钮监听
     * @return
     */
    public ItemsDialog setOnConfirmButton(String btnName,
                                          OnClickListener onClickListener) {
        this.mConfirmName = btnName;
        this.onConfirmListener = onClickListener;
        return this;
    }

    /**
     * 设置标题
     *
     * @param title
     * @return
     */
    public ItemsDialog setTitle(String title) {
        this.mTitle = title;
        return this;
    }

    public ItemsDialog setItems(List<String> items, OnClickListener listener) {
        this.mItems = items;
        this.onClickListener = listener;
        return this;
    }

    public String getScore() {
        return mScoreEt.getText().toString();
    }

    public EditText getScoreEt() {
        return mScoreEt;
    }

    public void setCleanOnClickListener(View.OnClickListener listener) {
        if (listener != null) {
            mCleanTv.setVisibility(View.VISIBLE);
            mCleanTv.setOnClickListener(listener);
        } else {
            mCleanTv.setVisibility(View.GONE);
        }
    }

    @Override
    public void show() {
        createDialog();
        super.show();
    }

    private ItemsDialog createDialog() {

        if (mTitle == null) {
            mTitleTv.setVisibility(View.GONE);
        } else {
            mTitleTv.setText(mTitle);
        }

        if (ScoreIsPut) {
            mScoreLl.setVisibility(View.VISIBLE);
        }
        if (topicType) {
            mScoreEt.setInputType(InputType.TYPE_CLASS_TEXT);
        }
        if (mItems == null) {
            mSingleLv.setVisibility(View.GONE);
        } else {
            mSingleLv.setAdapter(singleAdapter);
        }

        if (onClickListener != null) {
            mSingleLv
                    .setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                                View view, int position, long id) {
                            onClickListener.onClick(ItemsDialog.this, position);
                            ItemsDialog.this.dismiss();
                        }
                    });
        }

        if (onConfirmListener != null) {
            mSuerBt.setText(mConfirmName);
            mSuerBt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onConfirmListener
                            .onClick(ItemsDialog.this, BUTTON_CONFIRM);
                    ItemsDialog.this.dismiss();
                }
            });
        } else {
//			mSuerBt.setVisibility(View.GONE);
        }

        return this;
    }

    public ListView getListView() {
        return mSingleLv;
    }

    BaseAdapter singleAdapter = new BaseAdapter() {

        @Override
        public int getCount() {
            return mItems.size();
        }

        @Override
        public Object getItem(int position) {
            return mItems.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            TextView tv = new TextView(mContext);
            tv.setWidth(LayoutParams.MATCH_PARENT);
            tv.setHeight((int) ResourcesUtil.getDimension(mContext,
                    R.dimen.dialog_single_item_height));
            tv.setPadding(10, 0, 10, 0);
            tv.setTextSize(15);
            tv.setGravity(Gravity.CENTER_VERTICAL);
            tv.setTextColor(Color.BLACK);
            tv.setText(mItems.get(position));
            tv.setBackgroundDrawable(mContext.getResources().getDrawable(
                    R.drawable.message_double_selector));
            return tv;
        }
    };

}
